// import CryptoJS from "crypto-js"
import token from "@/api/token"
import axios from "axios"
import type { AxiosInstance, AxiosError, AxiosResponse } from 'axios'
import { ElMessage } from "element-plus"

const service: AxiosInstance = axios.create({
  timeout: 30000
})

service.interceptors.response.use((response: AxiosResponse) => {
  // const { code, message, data } = response.data

  // 根据自定义错误码判断请求是否成功

    return response
}, (error: AxiosError) => {
  // 处理 HTTP 网络错误
  let message = ''
  // HTTP 状态码
  const status = error.response?.status
  switch (status) {
    case 401: message = 'token 失效，请重新登录'
      break;
    case 403: message = '拒绝访问'
      break;
    case 404: message = '请求地址错误'
      break;
    case 500: message = '服务器故障'
      break;
    default: message = '网络连接故障'
  }
  console.log(message)
  ElMessage.error(message)
  return Promise.reject(error)
})


interface param {
  [key: string]: String | undefined
}

function getUrl() {
  if (location.hostname.indexOf("gitee") != -1) return "https://39.107.221.29/api"
  //码云上直接写死"https://39.107.221.29/api"
  return (location.protocol + "//" + location.hostname) + (location.hostname == "39.107.221.29" ? "/api" : ":9025")
}

function param2(url: String, param: param) {
  url += "?"
  for (let i in param) {
    url += i + "=" + param[i] + "&"
  }
  return getUrl() + url
  //http://39.107.221.29:3000
}


function myget(url: String, param: param) {
  param.tToken = token.getTimer()
  param.uToken = token.getToken()
  // console.log(param)
  return service(param2(url, param as param), {
    method: "GET",
    headers: { timer: token.getTimer() }
  })
}

function mypost(url: String, param: param, body: Object) {
  param.tToken = token.getTimer()
  param.uToken = token.getToken()
  return service(param2(url, param), {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    data: JSON.stringify(body)
  })
}

function myupload(url: String, param: param, body: Object) {
  param.timer = token.getTimer()
  return service(param2(url, param), {
    method: "POST",
    headers: {
      'Accept': 'application/json',
    },
    data: JSON.stringify(body)
  })
}


// function mye100(url,body) {
//   body.method="POST"
//   body.mode= "no-cors"
//   body.headers={
//          Host: 'www.e100soft.com',
//          'Proxy-Connection': 'keep-alive',
//          'Content-Length': '1542756',
//          'Cache-Control': 'max-age=0',
//          'Upgrade-Insecure-Requests': '1',
//          Origin: 'http://127.0.0.1:8080',
//          'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary5ADIauiqXScF3yGp',
//          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.49',
//          Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
//          Referer:' http://127.0.0.1:8080/',
//          'Accept-Encoding': 'gzip, deflate',
//          'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',

//       }
//   return fetch(url, body).then((res) => {
//     console.log(5,res.text())
//     return res.text()
//   })
// }
export default {
  myget,
  mypost,
  myupload,
  getUrl
}
